<?php
/**
 * Created by PhpStorm.
 * User: xuho
 * Date: 2020-09-02
 * Time: 17:10
 */

namespace extend\payment;


class FuYouWithdrawService extends FuYouBaseService
{
    public $domain = '';

    // 订单类型：ALIPAY-支付宝，WECHAT-公众号，UNIONPAY-银联二维码，BESTPAY-翼支付
    public $orderType = ['1' => 'WECHAT', '2' => 'ALIPAY', '3' => 'UNIONPAY', '4' => 'BESTPAY'];
    // 支付类型：JSAPI-公众号支付、APP-app 支付、FWC-支付宝服务窗、QQ-QQ 页面支付、QQJSAPI-QQJSAPI、LETPAY-小程序
    public $tradeType = ['1' => 'JSAPI', '2' => 'FWC', '3' => 'APP', '4' => 'QQ', '5' => 'QQJSAPI', '6' => 'LETPAY'];

    /**
     * 查询可提现资金信息
     * @param $request
     * @return mixed
     * @throws \Exception
     */
    public function withdrawAmountQuery($request)
    {
        // 接口类型
        $apiType = 'withdraw';

        // 获取支付参数
        $paramSign = $this->beforeRequest($request, $apiType);

        // 接口地址
        $urlApi = '/queryWithdrawAmt';

        // 不参与校验字段
        $paramAttach['reserved_busi_type'] = $request['reserved_busi_type'] ?? '1';

        $response = $this->sendRequest($urlApi, $paramSign, $paramAttach);

        return $this->afterRequest($response, $apiType);
    }

    /**
     * 手续费信息
     * @param $request
     * @return mixed
     * @throws \Exception
     */
    public function feeAmountQuery($request)
    {
        // 接口类型
        $apiType = 'withdraw';

        // 获取支付参数
        $paramSign = $this->beforeRequest($request, $apiType);

        // 接口地址
        $urlApi = '/queryFeeAmt';

        // 必填数据
        $paramSign['amt'] = $request['amt'];

        // 不参与校验字段
        $paramAttach['reserved_busi_type'] = $request['reserved_busi_type'] ?? '1';

        $response = $this->sendRequest($urlApi, $paramSign, $paramAttach);

        return $this->afterRequest($response, $apiType);
    }

    /**
     * 发起提现
     * @param $request
     * @return mixed
     * @throws \Exception
     */
    public function withDraw($request)
    {
        // 接口类型
        $apiType = 'withdraw';

        // 获取支付参数
        $paramSign = $this->beforeRequest($request, $apiType);
        // 接口地址
        $urlApi = '/withdraw';

        // 必填数据
        $paramSign['amt'] = $request['amt'];
        $paramSign['fee_amt'] = $request['fee_amt'];
        // 1-未结提现 2-已结提现
        $paramSign['txn_type'] = $request['txn_type'];

        $response = $this->sendRequest($urlApi, $paramSign);

        return $this->afterRequest($response, $apiType);
    }

    /**
     * 资金划拨查询
     * @param $request
     * @return mixed
     * @throws \Exception
     */
    public function capitalDetailQuery($request)
    {
        // 接口类型
        $apiType = 'settle';

        // 获取支付参数
        $paramSign = $this->beforeRequest($request, $apiType);
        // 接口地址
        $urlApi = '/queryChnlPayAmt';

        // 必填数据
        // 格式 yyyyMMdd
        $paramSign['start_date'] = $request['start_date'];
        $paramSign['end_date'] = $request['end_date'];
        $paramSign['start_index'] = $request['start_index'];
        $paramSign['end_index'] = $request['end_index'];

        $response = $this->sendRequest($urlApi, $paramSign);

        return $this->afterRequest($response, $apiType);
    }


    public function walletWithdraw($request)
    {
        // 接口类型
        $apiType = 'withdraw';

        // 获取支付参数
        $paramSign = $this->beforeRequest($request, $apiType);
        // 接口地址
        $urlApi = '/wallet/withdraw';

        // 必填数据
        $paramSign['mchnt_order_no'] = $request['mchnt_order_no'];
        $paramSign['amt'] = $request['amt'];
        $paramSign['fee_amt'] = $request['fee_amt'];
        // 3-次日到账 4-实时到账
        $paramSign['txn_type'] = $request['txn_type'];
        $paramSign['mchnt_order_no'] = $request['mchnt_order_no'];

        $response = $this->sendRequest($urlApi, $paramSign);

        return $this->afterRequest($response, $apiType);
    }

    public function walletWithdrawQuery($request)
    {
        // 接口类型
        $apiType = 'withdraw';

        // 获取支付参数
        $paramSign = $this->beforeRequest($request, $apiType);
        // 接口地址
        $urlApi = '/wallet/queryWithdrawT0';

        // 必填数据
        $paramSign['mchnt_order_no'] = $request['mchnt_order_no'];
        $paramSign['date'] = $request['date'];
        $paramSign['start_index'] = $request['start_index'];
        $paramSign['end_index'] = $request['end_index'];

        // 不参与校验字段
        $paramAttach['reserved_withdraw_no'] = $request['reserved_withdraw_no'] ?? '1';

        $response = $this->sendRequest($urlApi, $paramSign);

        return $this->afterRequest($response, $apiType);
    }


    public function beforeRequest($request)
    {
//        $this->domain = $request['domain'];
        $this->domain = 'https://scan-rim-test.fuioupay.com';
        $this->privateKey = $request['private_key'];

        // 商户号
        $paramSign['mchnt_cd'] = $request['mchnt_cd'];
        // 随机字符串
        $paramSign['random_str'] = nonceStr();
        // 机构号
        $paramSign['ins_cd'] = $request['ins_cd'];


        return $paramSign;
    }


    /**
     * 返回结果
     * @param $response
     * @param string $response_type
     *  共4种类型 ： order_create、order、withdraw、settle
     * @return mixed
     * @throws \Exception
     */
    public function afterRequest($response, $response_type)
    {
        $response = xml2array($response);
        var_dump($response);
        exit;
        if (strpos($response_type, 'order') !== false) {
            // 订单类型
            if ($response['result_code'] == '000000') {
                $result['status'] = '1';
                $result['msg'] = 'success';
                $result['data'] = $response;
            } else {
                $result['status'] = '0';
                $result['msg'] = $response['result_msg'];
            }
        } else if ($response_type == 'withdraw') {
            // 提现类型
            if ($response['result_code'] == 'SUCCESS') {
                $result['status'] = '1';
                $result['msg'] = 'success';
                $result['data'] = $response;
            } else {
                $result['status'] = '0';
                $result['msg'] = $response['return_msg'];
            }
        } else {
            $result['status'] = '1';
            $result['msg'] = 'success';
            $result['data'] = $response;
        }
        return $result;
    }
}